package com.mrzhou.study.struct.support;

/**
 * 数字栈: 用来保存算术中的数字数据
 */
public class NumStack {

    private int[] stack; // 数组栈
    private int top; // 栈顶指针
    private int maxSize; // 栈的最大容量

    public NumStack(int maxSize) {
        this.maxSize = maxSize;
        this.stack = new int[maxSize];
        top = -1;
    }

    public void push(int elem) {
        if(isFull()) { // 压栈时判断栈空间是否已满
            System.out.printf("超过栈最大的索引长度, 最大索引: %d\t入参索引: %d",  maxSize);
            return;
        }
        top++; // 指针先后移
        stack[top] = elem;
    }

    public int pop() {
        if(isEmpty()) {
            return -1;
        }
        int elem = stack[top];
        top--;
        return elem;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer("[");
        int cur = this.top;
        while(cur > -1) {
            if(cur == 0) {
                sb.append(stack[cur]);
            } else {
                sb.append(stack[cur]).append(", ");
            }
            cur--;
        }
        return sb.append("]").toString();
    }

    public boolean isDigit(char c) {
        return c >= '0' && c <= '9' ;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

}
